﻿using System;
using Examine;
using Umbraco.Core.Models;
using Umbraco.Web;
using umbraco;
using umbraco.interfaces;

namespace nForum.BusinessLogic.Models
{
    public class NodeMapper
    {
        #region Categories


        public ForumCategory MapForumCategory(IPublishedContent nodetomap)
        {
            if (nodetomap == null) return null;
            var fCat = new ForumCategory
            {
                Id = nodetomap.Id,
                CreatedOn = nodetomap.CreateDate,
                Name = nodetomap.Name,
                Url = nodetomap.Url,
                Description = nodetomap.GetPropertyValue<string>("forumCategoryDescription"),
                IsMainCategory = nodetomap.GetPropertyValue<bool>("forumCategoryIsMainCategory"),
                IsPrivate = nodetomap.GetPropertyValue<bool>("forumCategoryIsPrivate"),
                KarmaAccessAmount = nodetomap.GetPropertyValue<int>("forumCategoryPermissionKarmaAmount"),
                KarmaPostAmount = nodetomap.GetPropertyValue<int>("forumCategoryPostPermissionKarmaAmount"),
                ParentId = nodetomap.Parent.Id,
                ParentCategoryId = nodetomap.GetPropertyValue<int>("forumCategoryParentID"),
                SortOrder = nodetomap.SortOrder
            };
            return fCat;
        }

        #endregion

        #region Posts

        public ForumPost MapForumPost(IPublishedContent nodetomap)
        {
            if (nodetomap == null) return null;
            var fCat = new ForumPost
            {
                Id = nodetomap.Id,
                CreatedOn = nodetomap.CreateDate,
                ParentId = nodetomap.Parent.Id,
                Name = nodetomap.Name,
                Url = nodetomap.Url,
                Content = Helpers.HtmlDecode(nodetomap.GetPropertyValue<string>("forumPostContent")),
                Owner = MembershipHelper.ReturnMember(nodetomap.GetPropertyValue<int>("forumPostOwnedBy")),
                LastEdited = Helpers.InternalDateFixer(nodetomap.GetPropertyValue<string>("forumPostLastEdited")),
                IsSolution = nodetomap.GetPropertyValue<bool>("forumPostIsSolution"),
                IsTopicStarter = nodetomap.GetPropertyValue<bool>("forumPostIsTopicStarter"),
                Karma = nodetomap.GetPropertyValue<int>("forumPostKarma"),
                VotedMembersIds = Helpers.StringArrayToIntList(nodetomap.GetPropertyValue<string>("forumPostUsersVoted")),
                ParentTopicId = nodetomap.GetPropertyValue<int>("forumPostParentID"),
                SortOrder = nodetomap.SortOrder
            };

            return fCat;
        }

        #endregion

        #region Topics

        public ForumTopic MapForumTopic(IPublishedContent nodetomap)
        {
            if (nodetomap == null) return null;
            var fCat = new ForumTopic
            {
                Id = nodetomap.Id,
                Url = nodetomap.Url,
                Name = nodetomap.Name,
                CreatedOn = nodetomap.CreateDate,
                ParentId = nodetomap.Parent.Id,
                CategoryId = nodetomap.GetPropertyValue<int>("forumTopicParentCategoryID"),
                Owner = MembershipHelper.ReturnMember(nodetomap.GetPropertyValue<int>("forumTopicOwnedBy")),
                IsClosed = nodetomap.GetPropertyValue<bool>("forumTopicClosed"),
                IsSolved = nodetomap.GetPropertyValue<bool>("forumTopicSolved"),
                IsSticky = nodetomap.GetPropertyValue<bool>("forumTopicIsSticky"),
                SubscriberIds = Helpers.StringArrayToIntList(nodetomap.GetPropertyValue<string>("forumTopicSubscribedList")),
                LastPostDate = Helpers.InternalDateFixer(nodetomap.GetPropertyValue<string>("forumTopicLastPostDate")),
                SortOrder = nodetomap.SortOrder
            };
            return fCat;
        }


        #endregion

        /// <summary>
        /// This just checks the field exists and then returns the value,
        /// lucene is a frickin pain in the arse
        /// </summary>
        /// <returns></returns>
        private string CheckFieldExists(SearchResult result, string keyName)
        {
            return result.Fields.ContainsKey(keyName) ? result.Fields[keyName] : "";
        }
    }
}
